<?php

class CategoryController extends CF_Controller_Action
{
	function init(){
		if ($this->getRequest()->isXmlHttpRequest()) {
            //if AJAX - swith of templates rendering
			$ajaxContext = $this->_helper->getHelper('AjaxContext');
			$ajaxContext
             ->addActionContext('delete', 'json')
             ->addActionContext('add', 'json')
             ->addActionContext('edit', array('json','html'))
             ->addActionContext('index', array('json','html'))
             ->initContext();
        }
	}

	public function indexAction()
	{
		$this->view->title = "Categories";
		$this->view->headTitle($this->view->title);

		$categoriesDb = new Project_Category_Table();
		$categories = $categoriesDb->getCategories();
		$this->view->categories = $categories;
		$form = new Application_Form_Category();
		$this->view->form = $form;
	}

	public function deleteAction()
	{
		$responseDB['category_id'] = $this->_getParam('id');

		$categoryDB = new Project_Category_Table();

		if($categoryDB->deleteCategory($responseDB['category_id'])){
			$responseOutput['delete_info'] = 'category '
			.' has been deleted successfully!';
			$responseOutput['flag'] = true;
		}else{
			$responseOutput['delete_info'] = 'deleting failed';
			$responseOutput['flag'] = false;
		}

		$this->_helper->json($responseOutput, true);

		$form = new Application_Form_Category();
		$this->view->form = $form;

	}

	public function editAction()
	{

		$responseDB['title'] = $this->getRequest()->getPost('title');
		$responseDB['category_id'] = $this->_getParam('id');
		$categoryDB = new Project_Category_Table();
		$categoryDB->updateCategory($responseDB);
		$responseOutput['edit_title'] = $responseDB['title'];
		$this->_helper->json($responseOutput, true);
		$form = new Application_Form_Category();
		$this->view->form = $form;

//		$form = new Application_Form_Category();
//
//		$formData = $this->getRequest()->getPost();
//		if ($this->getRequest()->isPost()) {
//			$formData = $this->getRequest()->getPost();
//			if ($form->isValid($formData)) {
//				$categoryData = array();
//				$categoryData['title'] = $form->getValue('title');
//				$categoryData['has_id'] = $form->getValue('has_id');
//				$categoryData['parent_id'] = $form->getValue('parent_id');
//				$categoryDB = new Project_Category_Table();
//				$categoryDB->addCategory($categoryData);
//				//for ajax output
//				$this->view->categories = $categoryDB->getCategories();
//				if(!$this->getRequest()->isXmlHttpRequest()){
//					$this->_helper->redirector('index','index');
//				}
//
//			}else{
//				//for ajax output
//				$this->view->formErrors = $form->getErrorMessages();
//			}
//		}


	}

	public function addAction(){

		$responseDB['title'] = $this->getRequest()->getPost('title');
		$responseDB['has_child'] = 0;//it is child with no its children
		$responseDB['parent_id'] = (int)$this->_getParam('id');
		$categoryDB = new Project_Category_Table();
		$categoryDB->addCategory($responseDB);
		$responseOutput['add_title'] = $responseDB['title'];

		$this->_helper->json($responseOutput, true);

		$form = new Application_Form_Category();
		$this->view->form = $form;
//
//		$formData = $this->getRequest()->getPost();
//		if ($this->getRequest()->isPost()) {
//			$parent_id = $this->_getParam('id');
//			$formData = $this->getRequest()->getPost();
//			if ($form->title->isValid()) {
//				$categoryData = array();
//				$categoryData['title'] = $form->getValue('title');
//				$categoryData['has_id'] = 0;//it is child with no its children
//				$categoryData['parent_id'] = $parent_id;
//				$categoryDB = new Project_Category_Table();
//				$categoryDB->addCategory($categoryData);
//				//for ajax output
//				$this->view->categories = $categoryDB->getCategories();
//				if(!$this->getRequest()->isXmlHttpRequest()){
//					$this->_helper->redirector('index','index');
//				}else{
//
//				}
//
//
//
//			}else{
//				//for ajax output
//				$this->view->formErrors = $form->getErrorMessages();
//			}
//		}
	}
}



